package com.xinxin.leetcode.problem594;

import com.xinxin.leetcode.common.CommonUtil;

import java.util.Arrays;

/**
 * @author ShiXinXin
 * @date 2020-02-07 15:10
 */
public class Solution {
    public int findLHS(int[] nums) {
        if (nums.length < 2) {
            return 0;
        }
        Arrays.sort(nums);
        int begin = 0, res = 0;
        for (int end = 0; end < nums.length; end++) {
            while (nums[end] - nums[begin] > 1) {
                begin++;
            }
            if (nums[end] - nums[begin] == 1) {
                res = Math.max(res, end - begin + 1);
            }
        }
        return res;
    }

    public static void main(String[] args) {
        System.out.println(new Solution().findLHS(CommonUtil.createArr(1, 3, 2, 2, 5, 2, 3, 7)));
    }
}
